#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class FileProvider : CY.GFive.Core.Providers.DALProvider.IFileProvider
	{
		#region Sql
		private static readonly string SqlSelectFile = "SELECT [Id], [FileCate],[UpLoadDate],[UserCode],[DepartCode],[FileCode] FROM [File] WHERE  [Id]=@Id";

		private static readonly string SqlInsertFile = "INSERT INTO [File]([FileCate],[UpLoadDate],[UserCode],[DepartCode],[FileCode]) values(@FileCate,@UpLoadDate,@UserCode,@DepartCode,@FileCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateFile = "UPDATE [File] SET [FileCate] = @FileCate,[UpLoadDate] = @UpLoadDate,[UserCode] = @UserCode,[DepartCode] = @DepartCode,[FileCode] = @FileCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteFile = "DELETE [File] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllFile = "SELECT [Id], [FileCate],[UpLoadDate],[UserCode],[DepartCode],[FileCode] FROM [File]";
		#endregion

		#region IFileProvider Members
		public Core.Business.File Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectFile);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.File file = new Core.Business.File();

				if (!reader.IsDBNull(0)) file.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) file.FileCate = reader.GetString(1);
				if (!reader.IsDBNull(2)) file.UpLoadDate = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) file.UserCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) file.DepartCode = reader.GetString(4);
				if (!reader.IsDBNull(5)) file.FileCode = reader.GetString(5);

				return file;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.File file)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@FileCate", SqlDbType.NVarChar, file.FileCate);
			sql.AddParameter("@UpLoadDate", SqlDbType.DateTime, file.UpLoadDate);
			sql.AddParameter("@UserCode", SqlDbType.NVarChar, file.UserCode);
			sql.AddParameter("@DepartCode", SqlDbType.NVarChar, file.DepartCode);
			sql.AddParameter("@FileCode", SqlDbType.NVarChar, file.FileCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertFile);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) file.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.File file)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@FileCate", SqlDbType.NVarChar, file.FileCate);
			sql.AddParameter("@UpLoadDate", SqlDbType.DateTime, file.UpLoadDate);
			sql.AddParameter("@UserCode", SqlDbType.NVarChar, file.UserCode);
			sql.AddParameter("@DepartCode", SqlDbType.NVarChar, file.DepartCode);
			sql.AddParameter("@FileCode", SqlDbType.NVarChar, file.FileCode);
			sql.AddParameter("@Id", SqlDbType.Int, file.Id);
			sql.ExecuteSql(SqlUpdateFile);
		}

		public void Delete(Core.Business.File file)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, file.Id);
			sql.ExecuteSql(SqlDeleteFile);
		}

		public IList<Core.Business.File> GetAllFile()
		{
			IList<Core.Business.File> filelist = new List<Core.Business.File>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllFile);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.File file = new Core.Business.File();

					if (!reader.IsDBNull(0)) file.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) file.FileCate = reader.GetString(1);
					if (!reader.IsDBNull(2)) file.UpLoadDate = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) file.UserCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) file.DepartCode = reader.GetString(4);
					if (!reader.IsDBNull(5)) file.FileCode = reader.GetString(5);

					file.MarkOld();
					filelist.Add(file);
				}
				reader.Close();
			}
			return filelist;
		}
		#endregion
	}
}
